package com.cms.web.queryAnalyse;

import java.util.Calendar;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.metadata.biz.MetaDataServiceMgr;
import com.metadata.bizmodel.SYS_MODEL;
import com.mrp.persistence.queryAnalyse.pageQuery.bean.EMPPageQuery;
import com.web.BaseController;

import com.cms.biz.queryAnalyse.CMSQueryAnalyseServiceMgr;
import com.efounder.builder.base.data.EFDataSet;
import com.efounder.builder.base.data.EFRowSet;
import com.efounder.builder.meta.bizmodel.SYS_MDL_CTN;
import com.efounder.eai.data.JParamObject;
import com.efounder.eai.data.JResponseObject;
import com.efounder.form.EFFormDataModel;
import com.efounder.form.server.provider.FormModelDataProvider;
import com.enums.EMPQueryParamEnum;

import dwz.common.util.DateUtil;

@Controller
@RequestMapping(value="/cmsSHFWQueryAnalyse")
public class EMPCMSSHFWWQueryAnalyseController extends BaseController{
	@Autowired
	private                CMSQueryAnalyseServiceMgr                       queryAnalyseServiceMgr;

	@Autowired
	private                    FormModelDataProvider                                formModelUtil;
	
	@Autowired
	private                       MetaDataServiceMgr                           metaDataServiceMgr;

	@Autowired
	public HttpSession session;

	/**
	 * 材料明细查询参数选择界面
	 * @param F_CKBH
	 * @param model
	 * @return
	 */
	@RequestMapping("/preQueryForm/SHFWAnalyseQueryPage")
	public String materialQueryPage(@RequestParam String action, @RequestParam String navTabId, @RequestParam String title, @RequestParam String returnURL, Model model) {

		model.addAttribute("F_QSRQ", DateUtil.date3String("yyyy") + "-01-01");
		model.addAttribute("F_JZRQ", DateUtil.date2String(DateUtil.getLastDayOfMonth(Calendar.getInstance()), "yyyy-MM-dd"));		
		model.addAttribute("action", action);
		model.addAttribute("navTabId", navTabId);
		model.addAttribute("title", title);
		return returnURL;
	}
	
	/**
	 * 生产单剩余数量
	 * @param F_CKBH
	 * @param model
	 * @return
	 */
	@RequestMapping("/SHFWAnalyseQuery")
	public String SHFWQueryAnalyse(@RequestParam String F_QSRQ, @RequestParam String F_JZRQ, @RequestParam String F_HTBH, 
			                       @RequestParam String F_XMBH, @RequestParam String F_TXM, EMPPageQuery page, 
			                       @RequestParam String F_HTMC, @RequestParam String F_XMMC, Model model) {
		JParamObject           po = JParamObject.Create();
		EFDataSet    queryDataSet = null;
		EFRowSet      queryRowSet = null;
		String         BILL_WHERE = " SHFWXX.F_HTBH like '" + F_HTBH + "%' and SHFWXX.F_XMBH like '" + F_XMBH + "%' and "
		                          + " SHFWXX.F_ZDRQ >= '" + F_QSRQ + "' and SHFWXX.F_ZDRQ <= '" + F_JZRQ + "' and F_DJZT = '1'";
		
		if(!F_TXM.equals("")) {
			if(F_TXM.length() > 6) {
				BILL_WHERE += " and SHFWXX.F_KJQJ = '" + F_TXM.substring(0, 6) + "' and SHFWXX.F_DJBH = '" + F_TXM.substring(6) + "'";
			} else {
				BILL_WHERE += " and SHFWXX.F_KJQJ = '" + F_TXM + "'";
			}
		}	
		
		po.SetValueByParamName("CTN_FCT1", "SHFWXX");
		po.SetValueByParamName("BILL_WHERE", BILL_WHERE);
		po.SetValueByParamName("BILL_ORDER", "SHFWXX.F_KJQJ desc, SHFWXX.F_DJBH desc");
		po.setValue(EMPQueryParamEnum.PAGEQUERY, page);
		
		JResponseObject        RO = queryAnalyseServiceMgr.searchWXSQ(po);
		queryRowSet = (EFRowSet) RO.getResponseObject();
		
		queryDataSet = queryRowSet.getDataSet(EMPQueryParamEnum.QUERYRESULT);
		page = (EMPPageQuery) queryRowSet.getExtProperty(EMPQueryParamEnum.PAGEQUERY, null);
		model.addAttribute("queryDataSet", queryDataSet);
		model.addAttribute("page", page);
		model.addAttribute("F_QSRQ", F_QSRQ);
		model.addAttribute("F_JZRQ", F_JZRQ);
		model.addAttribute("F_HTBH", F_HTBH);
		model.addAttribute("F_XMBH", F_XMBH);
		model.addAttribute("F_HTMC", F_HTMC);
		model.addAttribute("F_XMMC", F_XMMC);
		model.addAttribute("F_TXM", F_TXM);
		return "/cms/queryAnalyse/SHFW/shfwQueryList";
	}

	/**
	 * 评审合同编辑
	 * @param model
	 * @return
	 */
	@RequestMapping("/productRequisitions/show/{F_GUID}")
	public String productRequisitionsEdit(@PathVariable("F_GUID") String F_GUID, Model model) {
		JParamObject           PO = JParamObject.Create();
		EFFormDataModel formModel;

		try {
			//加载模型数据
			PO.SetValueByParamName(SYS_MODEL.MODEL_ID, "SHFWModel");
			PO.SetValueByParamName(SYS_MODEL.SERVICE_ID, "FormProvider");
			PO.SetValueByParamName("BILL_WHERE", " ####.F_GUID = '" + F_GUID + "'");
			PO.SetValueByParamName(SYS_MDL_CTN._BIZ_CTN_TYPE_JIDS_ + "_SHFWMX_EXT_COLUMN", " F_CPBH.F_GGXH as F_GGXH");
			formModel = formModelUtil.loadForm(PO);
			EFDataSet itemCTN = metaDataServiceMgr.getSYS_MDL_CTN("SHFWModel", SYS_MDL_CTN._BIZ_CTN_TYPE_JIDS_);
			EFRowSet rowset = formModel.getBillDataSet().getRowSet(0);
			EFDataSet dataset = rowset.getDataSet(itemCTN.getRowSet(0).getString("CTN_FCT1", ""));

			model.addAttribute("dictObject", rowset);
			model.addAttribute("dataset", dataset);			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "/cms/queryAnalyse/SHFW/form/form";
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	/**
	 * 材料明细查询参数选择界面
	 * @param F_CKBH
	 * @param model
	 * @return
	 */
	@RequestMapping("/preQueryForm/SHFWTZAnalyseQueryPage")
	public String SHFWAnalyseQueryPage(@RequestParam String action, @RequestParam String navTabId, @RequestParam String title, @RequestParam String returnURL, Model model) {

		model.addAttribute("F_QSRQ", DateUtil.date3String("yyyy") + "-01-01");
		model.addAttribute("F_JZRQ", DateUtil.date2String(DateUtil.getLastDayOfMonth(Calendar.getInstance()), "yyyy-MM-dd"));		
		model.addAttribute("action", action);
		model.addAttribute("navTabId", navTabId);
		model.addAttribute("title", title);
		return returnURL;
	}
	
	/**
	 * 生产单剩余数量
	 * @param F_CKBH
	 * @param model
	 * @return
	 */
	@RequestMapping("/SHFWTZAnalyseQuery")
	public String SHFWTZQueryAnalyse(@RequestParam String F_QSRQ, @RequestParam String F_JZRQ, 
			                         @RequestParam String F_XMBH, EMPPageQuery page, 
			                         @RequestParam String F_XMMC, Model model) {
		JParamObject           po = JParamObject.Create();
		EFDataSet    queryDataSet = null;
		EFRowSet      queryRowSet = null;
		String         BILL_WHERE = "";
		String          BILL_LEFT = "";
		
		BILL_WHERE = " SHFWTZXX.F_ZDRQ >= '" + F_QSRQ + "' and SHFWTZXX.F_ZDRQ <= '" + F_JZRQ + "' and F_DJZT = '1' and "
				   + " exists(select 1 from HTXMZD where SHFWTZXX.F_XMBH = HTXMZD.F_XMBH and "
				   + " HTXMZD.F_XMMC LIKE '%" + F_XMMC + "%')";
		
//		BILL_LEFT = " LEFT JOIN HTXMZD F_XMBH ON SHFWTZXX.F_XMBH = F_XMBH.F_XMBH and F_XMBH.F_XMMC LIKE '%" + F_XMMC + "%'"
//				  + " LEFT JOIN HYBMZD F_CODE ON SHFWTZXX.F_CODE = F_CODE.F_BMBH"
//				  + " LEFT JOIN BSUSER F_ZDR  ON SHFWTZXX.F_ZDR = F_ZDR.USER_ID";
//		
		po.SetValueByParamName("CTN_FCT1", "SHFWTZXX");
		po.SetValueByParamName("BILL_WHERE", BILL_WHERE);
		po.SetValueByParamName("BILL_LEFT", BILL_LEFT);
		po.SetValueByParamName("BILL_ORDER", "SHFWTZXX.F_KJQJ desc, SHFWTZXX.F_DJBH desc");
		po.setValue(EMPQueryParamEnum.PAGEQUERY, page);
		
		JResponseObject        RO = queryAnalyseServiceMgr.searchWXSQ(po);
		queryRowSet = (EFRowSet) RO.getResponseObject();
		
		queryDataSet = queryRowSet.getDataSet(EMPQueryParamEnum.QUERYRESULT);
		page = (EMPPageQuery) queryRowSet.getExtProperty(EMPQueryParamEnum.PAGEQUERY, null);
		model.addAttribute("queryDataSet", queryDataSet);
		model.addAttribute("page", page);
		model.addAttribute("F_QSRQ", F_QSRQ);
		model.addAttribute("F_JZRQ", F_JZRQ);
		model.addAttribute("F_XMBH", F_XMBH);
		model.addAttribute("F_XMMC", F_XMMC);
		return "/cms/queryAnalyse/SHFWTZ/shfwQueryList";
	}

	/**
	 * 评审合同编辑
	 * @param model
	 * @return
	 */
	@RequestMapping("/productNotice/show/{F_GUID}")
	public String productNoticeEdit(@PathVariable("F_GUID") String F_GUID, Model model) {
		JParamObject           PO = JParamObject.Create();
		EFFormDataModel formModel;

		try {
			//加载模型数据
			PO.SetValueByParamName(SYS_MODEL.MODEL_ID, "SHFWTZModel");
			PO.SetValueByParamName(SYS_MODEL.SERVICE_ID, "FormProvider");
			PO.SetValueByParamName("BILL_WHERE", " ####.F_GUID = '" + F_GUID + "'");
			PO.SetValueByParamName(SYS_MDL_CTN._BIZ_CTN_TYPE_JIDS_ + "_SHFWTZMX_EXT_COLUMN", " F_CPBH.F_GGXH as F_GGXH, F_CPBH.F_JLDW as F_JLDW");
			formModel = formModelUtil.loadForm(PO);
			EFDataSet itemCTN = metaDataServiceMgr.getSYS_MDL_CTN("SHFWTZModel", SYS_MDL_CTN._BIZ_CTN_TYPE_JIDS_);
			EFRowSet rowset = formModel.getBillDataSet().getRowSet(0);
			EFDataSet dataset = rowset.getDataSet(itemCTN.getRowSet(0).getString("CTN_FCT1", ""));

			model.addAttribute("dictObject", rowset);
			model.addAttribute("dataset", dataset);			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "/cms/queryAnalyse/SHFWTZ/form/form";
	}
}
